<template>
  <div class="wrap">
    <div class="show-title">
      <span>主机列表</span>
    </div>
    <!-- 搜索 -->
    <div class="searchBox">
      <el-input
        prefix-icon="el-icon-search"
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width: 30%"
        clearable
      ></el-input>
    </div>
    <!-- 列表 -->
    <el-table
      ref="table"
      :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
      border
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="changeSort"
      :row-key="getRowKeys"
      :cell-style="changeCellStyle"
    >
      <!-- <el-table-column
        type="selection"
        width="40"
        align="center"
        :reserve-selection="true"
        :key="Math.random()"
      ></el-table-column> -->
      <el-table-column
        prop="sn"
        label="标识ID"
        min-width="250"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope"
          ><div @click="goDevice2(scope.row)">
            {{ scope.row.sn }}
          </div></template
        >
      </el-table-column>
      <el-table-column
        prop="alias"
        label="名称"
        min-width="250"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <div @click="goHostDevice(scope.row)">
            {{ scope.row.alias }}
          </div>
        </template>
        <!-- <template slot-scope="scope">
          <div
            v-if="scope.row.alias == '流媒体主机'"
            @click="goHostDevice(scope.row)"
            v-permissions="{
              url: ['host/device/list'],
              index: scope.$index,
              key: 'hideSn',
              id: 'sn1',
            }"
          >
            {{ scope.row.alias }}
          </div>
          <div
            @click="goHostDevice(scope.row)"
            v-permissions="{
              url: ['host/device/list'],
              index: scope.$index,
              key: 'hideSn',
              id: 'sn2',
            }"
          >
            {{ scope.row.alias }}
          </div>
        </template> -->
      </el-table-column>
      <!-- <el-table-column
        prop="host_type"
        label="类型"
        min-width="120"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.host_type == 0">心跳调度主机</span>
          <span v-if="scope.row.host_type == 1">心跳主机</span>
          <span v-if="scope.row.host_type == 2">流媒体主机</span>
          <span v-if="scope.row.host_type == 3">SP心跳主机</span>
          <span v-if="scope.row.host_type == 21">子流媒体主机</span>
          <span v-if="scope.row.host_type == 4">SIP主机</span>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="domain"
        label="注册地址"
        min-width="130"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.domain
        }}</template>
        <!-- <template slot-scope="scope">
          <span v-if="scope.row.host_type == 2"
            >{{ scope.row.domain }}
            <div class="s-tips">客户端访问地址</div></span
          >
          <span v-else-if="scope.row.host_type == 4"
            >{{ scope.row.domain }}
            <div class="s-tips">流媒体访问地址</div></span
          >
          <span v-else>{{ scope.row.domain }}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column
        prop="domain_lan"
        label="内网地址"
        min-width="130"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.domain_lan
        }}</template>
      </el-table-column> -->
      <el-table-column
        prop="port_control"
        label="控制"
        min-width="80"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_control
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_beating"
        label="心跳"
        min-width="80"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_beating
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_rtsp"
        label="RTSP"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_rtsp
        }}</template>
      </el-table-column>
      <!-- <el-table-column
        prop="port_hls"
        label="HLS"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_hls
        }}</template>
      </el-table-column> -->
      <el-table-column
        prop="port_rtmp"
        label="RTMP"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_rtmp
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_http_flv"
        label="HTTP-FLV"
        min-width="120"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_http_flv
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_ws"
        label="WEBSOCKET"
        min-width="140"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_ws
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_cms"
        label="CMS"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_cms
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_ydtsp"
        label="YDTSP"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_ydtsp
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_http"
        label="HTTP"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_http
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_https"
        label="HTTPS"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_https
        }}</template>
      </el-table-column>
      <el-table-column
        prop="port_cas"
        label="CAS"
        min-width="100"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.port_cas
        }}</template>
      </el-table-column>
      <el-table-column
        prop="owner_code"
        label="中心编码"
        min-width="100"
        align="center"
        fixed="right"
        :key="Math.random()"
        v-if="permissionData.hideOwnerCodeArr.length < 1"
      >
        <template slot-scope="scope">
          <div @click="goOwnerCode(scope.$index, scope.row)">
            <span
              v-if="scope.row.host_type == 2"
              v-permissions="{
                url: ['host/update/ownercode'],
                index: scope.$index,
                key: 'hideOwnerCode',
                id: 'ownercode',
              }"
            >
              <el-link type="primary">{{ scope.row.owner_code }}</el-link></span
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        min-width="100"
        align="center"
        fixed="right"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <div @click="statusFn(scope.$index, scope.row)">
            <span v-if="scope.row.status == -1 && scope.row.host_type == 2">
              <el-link type="success">已冻结</el-link>
            </span>
            <span v-else-if="scope.row.status == 0 && scope.row.host_type == 2">
              <el-link type="success">已关闭</el-link>
            </span>
            <span v-else-if="scope.row.status == 1 && scope.row.host_type == 2">
              <el-link type="success">已激活</el-link>
            </span>
            <span v-else>
              <el-link type="warning">-</el-link>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="级联"
        min-width="100"
        align="center"
        fixed="right"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <div @click="jilianFn(scope.$index, scope.row)">
            <span v-if="scope.row.io_type == 0 && scope.row.host_type == 2">
              <el-link type="success">接入和发布</el-link>
            </span>
            <span
              v-else-if="scope.row.io_type == 1 && scope.row.host_type == 2"
            >
              <el-link type="success">仅接入</el-link>
            </span>
            <span
              v-else-if="scope.row.io_type == 2 && scope.row.host_type == 2"
            >
              <el-link type="success">仅发布</el-link>
            </span>
            <span v-else>
              <el-link type="warning">-</el-link>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        class="play-box"
        label="操作"
        min-width="220"
        align="center"
        fixed="right"
        :key="Math.random()"
        v-if="permissionData.hideActionArr.length < 2"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="small"
            v-if="scope.row.host_type == 2"
            type="warning"
            plain
            @click="enableFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['host/update/status'],
              index: scope.$index,
              key: 'hideAction',
              id: 'jihuo',
            }"
            >激活</el-button
          >
          <el-button
            size="small"
            v-if="scope.row.host_type == 2"
            type="success"
            plain
            @click="frozenFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['host/update/status'],
              index: scope.$index,
              key: 'hideAction',
              id: 'dongjie',
            }"
            >冻结</el-button
          >
          <el-button
            size="small"
            v-if="scope.row.host_type == 2"
            type="danger"
            plain
            @click="closeFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['host/update/status'],
              index: scope.$index,
              key: 'hideAction',
              id: 'guanbi',
            }"
            >关闭</el-button
          > -->
          <el-button
            size="small"
            type="primary"
            @click="updateParamFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['host/param/update'],
              index: scope.$index,
              key: 'hideAction',
              id: 'peizhi',
            }"
            >配置参数</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="delRow(scope.$index, scope.row)"
            v-permissions="{
              url: ['host/delete'],
              index: scope.$index,
              key: 'hideAction',
              id: 'shanchu',
            }"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      right
      background
      layout="total,prev, pager, next, sizes,jumper"
      :page-sizes="pageSizesArr"
      :page-size="pagesize"
      :total="tableData.length"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <!-- 列表中sip，弹出框 -->
    <SipModify
      v-if="dialogVisibleAudit.show"
      :dialogVisibleAudit="dialogVisibleAudit"
      :sn="sn"
      :alias="alias"
    />
    <!-- 中心编码，弹窗 -->
    <OwnerCode
      v-if="dialogVisible_owner_code.show"
      :dialogVisible_owner_code="dialogVisible_owner_code"
      :sn="sn"
      :alias="alias"
      :owner_code="owner_code"
      @refreshList="fnCenterRefreshList"
    />
    <!-- 状态，弹窗 -->
    <StatusModify
      v-if="dialogVisible_status.show"
      :dialogVisible_status="dialogVisible_status"
      :sn="sn"
      :alias="alias"
      :status="status"
      @refreshList="refreshList"
    />
    <!-- 级联，弹窗 -->
    <JiLianModify
      v-if="dialogVisible_jilian.show"
      :dialogVisible_jilian="dialogVisible_jilian"
      :sn="sn"
      :alias="alias"
      :io_type="io_type"
      @refreshList="refreshList"
    />
    <!-- 配置参数 -->
    <UpdateParam
      v-if="dialogVisible_param.show"
      :dialogVisible_param="dialogVisible_param"
      :sn="sn"
      :alias="alias"
    />
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import SipModify from "./coms/SipModify";
import StatusModify from "./coms/StatusModify.vue";
import OwnerCode from "./coms/OwnerCode.vue";
import JiLianModify from "./coms/JiLianModify.vue";
import UpdateParam from "./coms/UpdateParam.vue";
export default {
  name: "",
  props: {},
  components: { SipModify, OwnerCode, StatusModify, JiLianModify, UpdateParam },

  data() {
    return {
      sn: "",
      alias: "",
      status: "",
      owner_code: "",
      io_type: "",
      frozenTxt: "冻结",
      closeTxt: "关闭",
      enableTxt: "激活",
      currpage: 1, //分页
      pagesize: globalPageSizes[0], //初始显示，一页几个
      pageSizesArr: globalPageSizes, //globalPageSizes在public--config中全局配置，每页显示个数数组
      searchTableInfo: "", //搜索条件
      getSearchInfo: [], //搜索后，过滤后的tableData数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      checkAll: false, // 是否全选状态
      //table中sip弹窗
      dialogVisibleAudit: {
        show: false,
      },
      //table中中心编码弹窗
      dialogVisible_owner_code: {
        show: false,
      }, //table中,状态弹窗
      dialogVisible_status: {
        show: false,
      },
      //table中,级联弹窗
      dialogVisible_jilian: {
        show: false,
      },
      //table中,配置参数弹窗
      dialogVisible_param: {
        show: false,
      },
      //权限
      permissionData: {
        hideSnArr: [], // 注意这里数组命名，hideAction对应你的key，后面加上Arr【必须这样命名，否则指令获取不到数组】
        hideActionArr: [], // 注意这里数组命名，hideAction对应你的key，后面加上Arr【必须这样命名，否则指令获取不到数组】
        hideOwnerCodeArr: [],
      },
    };
  },
  computed: {
    // 根据计算属性模糊搜索
    tableData() {
      const searchTableInfo = this.searchTableInfo;
      //console.log(searchTableInfo);
      if (searchTableInfo) {
        return this.getSearchInfo.filter((item) => {
          return Object.keys(item).some((key) => {
            return (
              String(item[key]).toLowerCase().indexOf(searchTableInfo) > -1
            );
          });
        });
      }
      return this.getSearchInfo;
    },
    //刷新table
    submitData() {
      return this.$store.state.submitData;
    },
  },
  watch: {
    //刷新table
    submitData(newVal, oldVal) {
      if (newVal != oldVal) {
        //this.getList();
        this.currpage = 1;
      }
    },
  },
  created() {},
  mounted() {
    this.getList();
  },
  methods: {
    //获取初始列表数据
    getList() {
      postData("/adminweb/host/list", {})
        .then((res) => {
          //console.log(res);
          if (res.code == 0) {
            let hosts = res.datas.hosts;
            this.getSearchInfo = hosts;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //冻结按钮
    // frozenFn(index, row) {
    //   console.log(row);
    //   var sn = row.sn;
    //   postData("/adminweb/host/update/status", {
    //     sn: sn,
    //     status: -1,
    //   })
    //     .then((res) => {
    //       if (res.code == 0) {
    //         this.frozenTxt = "已冻结";
    //         this.$message.success("已冻结");
    //         this.getList();
    //       } else {
    //         this.$message.error(res.code + "，" + res.msg);
    //       }
    //     })
    //     .catch((res) => {
    //       this.$message.error(res.code + "，" + res.msg);
    //     });
    // },
    //关闭按钮
    // closeFn(index, row) {
    //   var sn = row.sn;
    //   postData("/adminweb/host/update/status", {
    //     sn: sn,
    //     status: 0,
    //   })
    //     .then((res) => {
    //       if (res.code == 0) {
    //         this.closeTxt = "已关闭";
    //         this.$message.success("已关闭");
    //         this.getList();
    //       } else {
    //         this.$message.error(res.code + "，" + res.msg);
    //       }
    //     })
    //     .catch((res) => {
    //       this.$message.error(res.code + "，" + res.msg);
    //     });
    // },
    //激活按钮
    // enableFn(index, row) {
    //   var sn = row.sn;
    //   postData("/adminweb/host/update/status", {
    //     sn: sn,
    //     status: 1,
    //   })
    //     .then((res) => {
    //       if (res.code == 0) {
    //         this.enableTxt = "已激活";
    //         this.$message.success("已激活");
    //         this.getList();
    //       } else {
    //         this.$message.error(res.code + "，" + res.msg);
    //       }
    //     })
    //     .catch((res) => {
    //       this.$message.error(res.code + "，" + res.msg);
    //     });
    // },

    //删除单行
    delRow(index, rows) {
      //  console.log(rows);
      var that = this;
      that
        .$confirm("此操作将删除主机:" + rows.alias + ", 是否继续?", "删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          postData("/adminweb/host/delete", {
            sn: rows.sn,
          })
            .then((res) => {
              that.logining = false;
              // console.log(res);
              if (res.code == "0") {
                that.$message.success("删除成功");
                that.getList();
              } else {
                that.$message.error(res.code + ":" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error(res);
            });
        })
        .catch((res) => {
          that.$message.success("已取消");
        });
    },
    //点击序列号，
    goDevice2(val) {
      // console.log(val);
      var sn = val.sn;
      if (val.host_type == 2 && val.io_type != 2) {
        //流媒体
        this.$router.push({
          path: "/hostList/HostDevice",
          query: { sn: sn },
        });
      } else if (val.host_type == 2 && val.io_type == 2) {
        this.$message.error("主机级联为[仅发布]类型下，不可点击！");
      } else if (val.host_type == 4) {
        //sip
        this.$router.push({
          path: "/hostList/SipDevice",
          query: { sn: sn },
        });
      }
    },
    //点击名称
    goHostDevice(val) {
      // console.log(val);
      var sn = val.sn;
      if (val.host_type == 4) {
        this.$set(this.dialogVisibleAudit, "show", true);
        this.sn = sn;
        this.alias = val.alias;
      }
    },
    //点击中心编码，弹窗
    goOwnerCode(index, row) {
      console.log(row);
      this.dialogVisible_owner_code = { show: true };
      this.sn = row.sn;
      this.alias = row.alias;
      this.owner_code = row.owner_code;
    },
    //点击状态，弹窗
    statusFn(index, row) {
      // console.log(row);
      this.dialogVisible_status = { show: true };
      this.sn = row.sn;
      this.alias = row.alias;
      this.status = row.status;
    },
    //点击级联，弹窗
    jilianFn(index, row) {
      // console.log(row);
      this.dialogVisible_jilian = { show: true };
      this.sn = row.sn;
      this.alias = row.alias;
      this.io_type = row.io_type;
    },
    //点击配置参数，弹窗
    updateParamFn(index, row) {
      this.dialogVisible_param = { show: true };
      this.sn = row.sn;
      this.alias = row.alias;
    },
    //table第一列小手样式
    changeCellStyle(row) {
      //console.log(row);
      if (row.columnIndex === 0 && row.row.host_type == "2") {
        return "color:#3182bd;cursor: pointer";
      }
      if (row.columnIndex === 0 && row.row.host_type == "4") {
        return "color:#3182bd;cursor: pointer";
      }
      if (row.columnIndex === 1 && row.row.host_type == "4") {
        return "color:#3182bd;cursor: pointer";
      }
    },
    // 获取row的key值
    getRowKeys(row) {
      return row.sn;
    },
    //点击 第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
    },
    //每页有几条
    handleSizeChange(psize) {
      this.pagesize = psize;
    },
    //操作多选
    handleSelectionChange(val) {
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      //  console.log(that.tableChecked);
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.batchDelectId.push(val[i]["sn"]);
        }
      }
    },
    //排序
    changeSort(val) {
      // console.log(val);
    },

    fnCenterRefreshList() {
      this.dialogVisible_owner_code = { show: false };
      this.getList();
    },

    refreshList() {
      this.getList();
    },
  },
};
</script>
<style scoped>
/* 更改table,表头背景 */
.wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.wrap {
  margin-top: 20px;
  background-color: #fff;
  padding: 15px;
  overflow: hidden;
}
.searchBox {
  margin-bottom: 20px;
}
.wrap >>> .el-pagination {
  float: right;
  margin-top: 30px;
}

.batch-btn-con {
  background: #fff;
  margin-top: 70px;
}
.wrap >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 15px;
}
/*title*/
.show-title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  background: #c0dee2;
  padding-left: 15px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  border-radius: 12px 12px 0 0;
  font-weight: bold;
}

.show-title::before {
  content: "";
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 27px;
}

.show-title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}
.s-tips {
  color: #989898;
  /* color: #676a6d; */
  font-size: 14px;
}
</style>
